package org.xg.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName RemoveCookieServlet
 * @date 2021/9/24 11:08
 **/
@WebServlet("/removeCookie")
public class RemoveCookieServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 获取cookie
        Cookie cookie = new Cookie("lastTime", "");
        // 设置有效时间为0，删除cookie
        cookie.setMaxAge(0);
        // 设置有效路径，必须与要删除的Cookie的路径一致
        cookie.setPath("/servletCookie");
        // 回写
        response.addCookie(cookie);
        // 重定向商品列表页面
        response.sendRedirect("/servletCookie/loginTime");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        doGet(request, response);
    }
}
